/*
 * CS_MARKET_MY_ITEM_CANCEL.cpp
 *
 *  Created on: 2013. 5. 22.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/MailBox.h"
#include "../../NLogic/Trade.h"
#include "../../NLogic/Static.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_MARKET_MY_ITEM_CANCEL(User * pUser, ::Network::Packet * pPacket)
{
/*	// 거래소 내 목록 취소
	CS_MARKET_MY_ITEM_CANCEL	0x100A
	{
		u64	item_seq;	// 마켓 seq
		u8 class_type;
		u8 sheet_type;
	}

	// 거래소 내 목록 취소
	SC_MARKET_MY_ITEM_CANCEL	0x100B
	{
		s16 ack;	 /// 성공(1),  실패(-1), ACK_MARKET_ALREADY_SELL	= 600,	// 아이템이 이미 판매

	}*/

	PREPARE_PACKET;

	uint64_t tradeSeq	= 0;
	char classType		= 0;
	char sheetType		= 0;

	*pPacket >> tradeSeq >> classType >> sheetType;

	DEBUGLOG("CS_MARKET_MY_ITEM_CANCEL : userseq=%d, tradeseq=%lld, class=%d, sheet=%d", pUser->GetUserSeq(), tradeSeq, classType, sheetType);

	NLogic::Trade * pTrade = pUser->GetTrade();
	if (NULL == pTrade)
	{
		DEBUGLOG("CS_MARKET_MY_ITEM_CANCEL : pTrade is NULL!");
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_MARKET_MY_ITEM_CANCEL);
		pUser->Send(pAck);
		return;
	}

	NLogic::Trade::Key clsKey;
	clsKey.tradeSeq		= tradeSeq;
	clsKey.classType	= static_cast<int>(classType);
	clsKey.sheetType	= static_cast<int>(sheetType);

	// 내목록에 없으면 이미 판매된 아이템
	if (false == pTrade->IsExistTradeItemData(clsKey))
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_MARKET_ALREADY_SELL);
		pAck->MakePacket(CMD_SC_MARKET_MY_ITEM_CANCEL);
		pUser->Send(pAck);
		return;
	}

	NLogic::Trade::TradeItem * pTradeItem = pTrade->GetTradeItemData(clsKey);
	if (NULL == pTradeItem)
	{
		DEBUGLOG("CS_MARKET_MY_ITEM_CANCEL : pTradeItem is NULL!");
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_MARKET_MY_ITEM_CANCEL);
		pUser->Send(pAck);
		return;
	}

	// 아이템의 상태를 확인한다.
	int itemStatus = pTradeItem->SelectDatabaseTradeItemStatus();

	if (0 > itemStatus || 2 <= itemStatus)
	{
		DEBUGLOG("CS_MARKET_MY_ITEM_CANCEL : INVALID STATUS=%d", itemStatus);
		return;
	}

	// 판매자가 취소하기 전에 판매 된 경우
	if (1 == itemStatus)
	{
		// 내목록에서 삭제
		pTrade->RemoveTradeItemData(clsKey);

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_MARKET_ALREADY_SELL);
		pAck->MakePacket(CMD_SC_MARKET_MY_ITEM_CANCEL);
		pUser->Send(pAck);
		return;
	}

	// Database에서 취소 처리
	if (false == pTradeItem->UpdateDatabaseCancelStatus())
	{
		DEBUGLOG("CS_MARKET_MY_ITEM_CANCEL : false == pTradeItem->UpdateDatabaseCancelStatus()");
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_DB_ERROR);
		pAck->MakePacket(CMD_SC_MARKET_MY_ITEM_CANCEL);
		pUser->Send(pAck);
		return;
	}


	// 취소한 아이템은 내 메일함으로 전송
	NLogic::MailBox::Mail::Data clsData;
	clsData.send_user_seq		= 0;
	clsData.send_user_nickname	= "GAMEVIL";
	clsData.recv_user_seq		= pUser->GetUserSeq();
	clsData.gift_status			= EM_GIFT_ITEM;
	clsData.item				= pTradeItem->GetData().item;
	clsData.message				= NResource::Static::InstancePtr()->GetMailMessageResource()->GetMessageTradeItemCancel();

	if (false == NLogic::NFunction::SendMail(pUser, clsData))
	{
		DEBUGLOG("CS_MARKET_MY_ITEM_CANCEL : false == NLogic::NFunction::SendMail(pUser, clsData)");
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_DB_ERROR);
		pAck->MakePacket(CMD_SC_MARKET_MY_ITEM_CANCEL);
		pUser->Send(pAck);
		return;
	}


	// 내목록에서 삭제
	pTrade->RemoveTradeItemData(clsKey);

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	pAck->MakePacket(CMD_SC_MARKET_MY_ITEM_CANCEL);
	pUser->Send(pAck);
}


} /*NCommand*/ } /*ProjectZ*/
